package servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import Jsonutil.JsonDateValueProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import dao.UserDao;
import entity.UserActiveSoft;
import entity.UserLicense;

public class QueryUserActiveSoftServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5713627652902849319L;

	public QueryUserActiveSoftServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();

		String deptName = request.getParameter("deptName");
		deptName = URLDecoder.decode(URLDecoder.decode(StringUtils.isEmpty(deptName) ? "" : deptName, "UTF-8"), "UTF-8");
		String userName = request.getParameter("userName");
		userName = URLDecoder.decode(URLDecoder.decode(StringUtils.isEmpty(userName) ? "" : userName, "UTF-8"), "UTF-8");
		String softName = request.getParameter("softName");
		softName = URLDecoder.decode(URLDecoder.decode(StringUtils.isEmpty(softName) ? "" : softName, "UTF-8"), "UTF-8");
		String status = request.getParameter("status");
		status = URLDecoder.decode(URLDecoder.decode(StringUtils.isEmpty(status) ? "" : status, "UTF-8"), "UTF-8");
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		UserDao userDao = null;
		
		try {
			userDao = new UserDao();
			List<UserActiveSoft> list = userDao.queryUserActiveSoft(userName, softName, status, deptName);
			if(list != null && list.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
				jsonObject.put("retCode", "1");
				jsonObject.put("userActiveSofts", jsonArray);
			} else {
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "无记录");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "数据库异常，请联系技术人员");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
	}

}
